import type { JSX } from "react"
import { useSelector } from "react-redux";
import { Navigate,useLocation } from "react-router";
import { message } from "antd";


interface RequireAuthProps{
    children: JSX.Element
}
const whitePages=["/index","/"]
function RequireAuth({children}:RequireAuthProps){
    const {pathname}= useLocation();
    const token= useSelector((state:any) => state.token);
    const menus= useSelector((state:any) => state.menus);
    if(!token){
        return <Navigate to="/login" replace />
    }
    if(whitePages.includes(pathname)){
        return children;
    }
    const isPermission= menus.some((item:any)=>item.url===pathname)
    if(!isPermission){
        return <Navigate to='/404' replace />
    }

    return children;

}
export default RequireAuth